package com.work.lxykeeper.module.main;


import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.work.lxykeeper.R;
import com.work.lxykeeper.base.BaseActivity;
import com.work.lxykeeper.cache.Ckey;
import com.work.lxykeeper.cache.SPutils;
import com.work.lxykeeper.databinding.ActivityMainzkyyBinding;
import com.work.lxykeeper.module.homepage.CaptureActivity;
import com.work.lxykeeper.module.pagemine.LoginActivity;
import com.work.lxykeeper.module.pagemine.ResetPasswordActivity;
import com.work.lxykeeper.utils.PermissionUtil;
import com.work.lxykeeper.utils.UIUtils;
import com.work.lxykeeper.view.CommonPopupWindow;
import com.work.lxykeeper.view.SignDialog;


/**
 * @author WZ 2020/11/30
 */
public class MainZkyyActivity extends BaseActivity {
    private static final int CAMERA_PERMISSIONS_REQUEST = 001001;
    private static Context context;
    private long exitTime = 0;
    private ImageView ivSetting;
    private ImageView ivCapture;
    private CommonPopupWindow commonPopupWindow;
    private ActivityMainzkyyBinding binding;

    @Override
    public void initView() {
        context = this;
        ivSetting = findViewById(R.id.iv_setting);
        ivCapture = findViewById(R.id.iv_capture);
        ivSetting.setOnClickListener(this);
        ivCapture.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        setPopupWindow();
    }

    @Override
    protected View getLayoutView() {
        binding= ActivityMainzkyyBinding.inflate(getLayoutInflater());
        return binding.getRoot();

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    /***
     * 退出应用
     */
    public void exit() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            UIUtils.showToast("再按一次退出应用");
            exitTime = System.currentTimeMillis();
        } else {
            exitApp();
        }
    }

    /**
     * 退出应用
     */
    public void exitApp() {
        finish();
    }

    public static Context getContext() {
        return context;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == CAMERA_PERMISSIONS_REQUEST) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED &&
                    ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                Intent intents = new Intent(this, CaptureActivity.class);
                startActivity(intents);
            } else {
                UIUtils.showToast("请打开摄像头权限");
            }
        }
    }

    @Override
    protected void processClick(View v) {
        super.processClick(v);
        switch (v.getId()) {
            case R.id.iv_setting:
                //设置
                commonPopupWindow.showAsDropDown(ivSetting);
                backgroundAlpha(0.6f);
                break;
            case R.id.iv_capture:
                //扫码
                openQrcode();
                break;
        }
    }

    //打开二维码扫描界面
    private void openQrcode() {
        PermissionUtil permissionUtil = new PermissionUtil(this);
        if (permissionUtil.requestCameraPermission(CAMERA_PERMISSIONS_REQUEST)) {
            Intent intents = new Intent(this, CaptureActivity.class);
            startActivity(intents);
        }
    }

    //选择家庭
    private void setPopupWindow() {
        commonPopupWindow = new CommonPopupWindow(this, R.layout.layout_setting);
        commonPopupWindow.setAnimationStyle(R.style.mypopwindowfromtop_anim_style);
        commonPopupWindow.setClippingEnabled(false);
        View view = commonPopupWindow.getMenuView();
        TextView tvResetpassword = view.findViewById(R.id.tv_resetpassword);
        TextView tvExit = view.findViewById(R.id.tv_exit);
        tvResetpassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //修改密码
                commonPopupWindow.dismiss();
                UIUtils.startActivity(ResetPasswordActivity.class);
            }
        });
        tvExit.setOnClickListener(new View.OnClickListener() {
            private SignDialog signDialog;

            @Override
            public void onClick(View v) {
                //退出登录
                if (signDialog == null) {//"确定退出登录?"
                    signDialog = new SignDialog.Builder(MainZkyyActivity.this).setTitleText("提示").setContentText("你确定要退出登录吗?").setLeftOnClick(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            signDialog.dismiss();
                        }
                    }).setRightOnClick(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            signDialog.dismiss();
                            SPutils.remove(Ckey.USERMESSAGE);
                            SPutils.remove(Ckey.UID);
                            SPutils.remove(Ckey.AESKEY);
                            SPutils.remove(Ckey.TIME);
                            SPutils.remove(Ckey.TOKEN);
                            UIUtils.startActivity(LoginActivity.class);
                            finish();
                        }
                    }).create();
                }
                commonPopupWindow.dismiss();
                signDialog.show();
            }
        });
        commonPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1.0f);
            }
        });
    }
}
